home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / SWar / Shots.p < prev    next >
Text File  |  1995-06-15  |  4KB  |  132 lines

  1. unit shots;
  2.  
  3. interface
  4.  
  5.     uses
  6. {$IFC UNDEFINED THINK_PASCAL}
  7.         Types, QuickDraw, Events, Menus, Dialogs, Fonts, Resources, Devices,
  8. {$ENDC}
  9.         Globals, Util, Ships;
  10.  
  11.     procedure InitShots;
  12.     procedure CreateShot (c: RGBColor; h, v, hv, vv: Integer);
  13.     procedure MoveShots;
  14.     procedure DrawShots;
  15.  
  16.  
  17. implementation
  18.  
  19.     procedure InitShots;
  20.         var
  21. { myWhite, myBlack, myRed, myBlue, myYellow, myGreen, myOrange, myGray, myDkBlue: }
  22. {   RGBColor extern;}
  23. {   gShotRecs[MAX_SHOTS]}
  24. {   , gOldShotRecs [ MAX_SHOTS ] : SHOTREC extern; }
  25.             i, nc: Integer;
  26.     begin
  27.         for i := 0 to MAX_SHOTS - 1 do
  28.             begin
  29.                 gShotRecs[i].where.v := -1;
  30.                 gShotRecs[i].where.h := -1;
  31.                 gShotRecs[i].vel.v := -1;
  32.                 gShotRecs[i].vel.h := -1;
  33.                 gShotRecs[i].lifeSpan := 0;
  34.                 gShotRecs[i].isAlive := FALSE;
  35.             end; (* for *)
  36.  
  37.     end; (* InitShots() *)
  38.  
  39.     procedure CreateShot (c: RGBColor; h, v, hv, vv: Integer);
  40.         var
  41.             i: Integer;
  42.     begin
  43.         for i := 0 to MAX_SHOTS - 1 do
  44.             if (not gShotRecs[i].isAlive) then
  45.                 begin
  46.                     gShotRecs[i].where.v := v;
  47.                     gShotRecs[i].where.h := h;
  48.                     gShotRecs[i].vel.v := vv;
  49.                     gShotRecs[i].vel.h := hv;
  50.                     gShotRecs[i].isAlive := TRUE;
  51.                     gShotRecs[i].lifeSpan := 20;
  52.                     gShotRecs[i].color := c;
  53.                     exit(CreateShot);
  54.                 end; (* if *)
  55.     end; (* CreateShot() *)
  56.  
  57.     procedure MoveShots;
  58.         var
  59.             i: Integer;
  60. { gShotRecs[MAX_SHOTS]}
  61. {   : }
  62. {   SHOTREC extern; }
  63.     begin
  64.         for i := 0 to MAX_SHOTS - 1 do
  65.             if (gShotRecs[i].isAlive) then
  66.                 begin
  67.                     if (gShotRecs[i].lifeSpan <> 0) then
  68.                         begin
  69.                             gShotRecs[i].lifeSpan := gShotRecs[i].lifeSpan - 1;
  70.                             gShotRecs[i].where.h := gShotRecs[i].where.h + gShotRecs[i].vel.h;
  71.                             gShotRecs[i].where.v := gShotRecs[i].where.v + gShotRecs[i].vel.v;
  72.                             if (gShotRecs[i].where.h > 637) then
  73.                                 begin
  74.                                     gShotRecs[i].vel.h := -gShotRecs[i].vel.h;
  75.                                     gShotRecs[i].where.h := 637;
  76.                                 end; (* if *)
  77.                             if (gShotRecs[i].where.h < 1) then
  78.                                 begin
  79.                                     gShotRecs[i].vel.h := -gShotRecs[i].vel.h;
  80.                                     gShotRecs[i].where.h := 1;
  81.                                 end; (* if *)
  82.                             if (gShotRecs[i].where.v > 477) then
  83.                                 begin
  84.                                     gShotRecs[i].vel.v := -gShotRecs[i].vel.v;
  85.                                     gShotRecs[i].where.v := 477;
  86.                                 end; (* if *)
  87.                             if (gShotRecs[i].where.v < 21) then
  88.                                 begin
  89.                                     gShotRecs[i].vel.v := -gShotRecs[i].vel.v;
  90.                                     gShotRecs[i].where.v := 21;
  91.                                 end; (* if *)
  92.                         end (* if *)
  93.                     else
  94.                         gShotRecs[i].isAlive := FALSE;
  95.                 end; (* if *)
  96.  
  97.     end; (* MoveShots() *)
  98.  
  99.     procedure DrawShots;
  100.         var
  101.             savePort: GrafPtr;
  102.             i, j: Integer;
  103.             dstRect: Rect;
  104.     begin
  105.         GetPort(savePort);
  106.         SetPort(GrafPtr(gOSPtr));
  107.         for i := 0 to MAX_SHOTS - 1 do
  108.             begin
  109.                 if gOldShotRecs[i].isAlive then
  110.                     SetCPixel(gOldShotRecs[i].where.h, gOldShotRecs[i].where.v, myBlack);
  111.                 if gShotRecs[i].isAlive then
  112.                     begin
  113.                         SetCPixel(gShotRecs[i].where.h, gShotRecs[i].where.v, gShotRecs[i].color);
  114.                         gOldShotRecs[i] := gShotRecs[i];
  115.                         dstRect.top := gShotRecs[i].where.v;
  116.                         dstRect.left := gShotRecs[i].where.h;
  117.                         dstRect.bottom := dstRect.top + 1;
  118.                         dstRect.right := dstRect.left + 1;
  119.                         for j := 0 to MAX_PLAYERS - 1 do
  120.                             if not ColorsEqual(gShotRecs[i].color, gShipRecs[j].color) then
  121.                                 if CheckForShipCollision(j, dstRect) then
  122.                                     begin
  123.                                         gShotRecs[i].isAlive := FALSE;
  124.                                         KillPlayer(j);
  125.                                     end; (* if *)
  126.                     end; (* if *)
  127.             end; (* for *)
  128.         SetPort(GrafPtr(savePort));
  129.  
  130.     end; (* DrawShots() *)
  131.  
  132. end.